/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package idea.sfo.viewer.designer;


import javax.swing.Action;
import org.openide.ErrorManager;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Node.Property;
import org.openide.nodes.PropertySupport;
import org.openide.nodes.Sheet;
import org.openide.util.lookup.Lookups;


/**
 *
 * @author renke002
 */
public class DesignerRowNode extends AbstractNode
{

    /**
     *
     * @param dr - DesignerRow object being wrapped by this Node
     * @param dcc -
     */
    DesignerRowNode( DesignerRow dr , DesignerColChildren dcc )
    {
        super( dcc , Lookups.singleton( dr ) );
        this.setDisplayName( dr.getName() );
    }


    /**
     * Since this class is extending an org.openide.nodes.AbstractNode, we need
     * to implement our own version of createSheet() to display the properties
     * we want the user to have access to.
     *
     * @return
     */
    @Override
    protected Sheet createSheet()
    {
        Sheet sheet = Sheet.createDefault();
        Sheet.Set set = Sheet.createPropertiesSet();
        DesignerRow dr = getLookup().lookup( DesignerRow.class );

        try
        {

            Property nameProp = new PropertySupport.Reflection<>( dr , String.class , "getName" , null );
            Property colsNumProp = new PropertySupport.Reflection<>( dr , Integer.class , "getNumberOfCols" , null );
            Property rowHeightProp = new PropertySupport.Reflection<>( dr , Long.class , "getRowHeight" , null );
            Property rowWidthProp = new PropertySupport.Reflection<>( dr , Long.class , "getRowWidth" , null );

            nameProp.setName( "ID" );
            colsNumProp.setName( "Cols" );
            rowHeightProp.setName( "Height" );
            rowWidthProp.setName( "Width" );

            set.put( nameProp );
            set.put( colsNumProp );
            set.put( rowHeightProp );
            set.put( rowWidthProp );

        }
        catch ( NoSuchMethodException ex )
        {
            ErrorManager.getDefault();
        }

        //
        sheet.put( set );
        //
        return sheet;
    }


    /**
     * Equality between two object of this class is solely based on their row
     * numbers, I.E. if two object are in the same row, they will be treated as
     * being the same object.
     *
     * @param obj
     * @return
     */
    @Override
    public boolean equals( Object obj )
    {
        if ( obj == null )
        {
            return false;
        }
        if ( getClass() != obj.getClass() )
        {
            return false;
        }
        final DesignerRowNode other = (DesignerRowNode) obj;
        if ( other.hashCode() != this.hashCode() )
        {
            return false;
        }
        return true;
    }


    /**
     * Called when right-clicking on a node.
     *
     * @param context
     * @return
     */
    @Override
    public Action[] getActions( boolean context )
    {
        Action[] superActs = super.getActions( context );
        System.out.println( "getActions" + superActs );
        return superActs;
    }


    /**
     *
     * @return
     */
    public DesignerRow getDesignerRow()
    {
        return getLookup().lookup( DesignerRow.class );
    }


    /**
     * Default function called when double-clicking on a node.
     *
     * @return
     */
    @Override
    public Action getPreferredAction()
    {
        Action superAct = super.getPreferredAction();
        System.out.println( "getPreferredAction" + superAct );
        return superAct;
    }


    /**
     * @return the row this object has been placed on.
     */
    public Integer getRowNumber()
    {
        return this.getDesignerRow().getRowNumber();
    }


    /**
     * Local implementation of the hashCode() function based on the object's row
     * number.
     *
     * @return
     */
    @Override
    public int hashCode()
    {
        /* A DesignerRow should be uniquely identified by its row number so it's
         * practical in this case to use its row number.
         */
        return this.getRowNumber();
    }

}
